/*
Date: October 2025
Project: Income and Child Maltreatment: Evidence from a Discontinuity in Tax Benefits
Author: Katherine Rittenhouse
Purpose: This file takes in data at the individual - foster care placement level and creates a dataset at the individual level.
Files in: ucb_fc
Files out: FCwide; CPS_FCID
*/

clear all
set more off

use "ucb_fc.dta",clear
replace PE_E_DT = mdy(12,31,2020) if PE_E_DT==.

drop if PE_S_DT<mdy(1,1,1999)
gen byte age_at_pl = floor((PE_S_DT - BIRTH_DT)/365)
gen byte age_end_pl = floor((PE_E_DT - BIRTH_DT)/365)
gen pl_days = PE_E_DT - PE_S_DT 
duplicates drop FKCLIENT_T PE_S_DT PE_E_DT, force
sort FKCLIENT PE_S_DT
by FKCL: gen epnum = _n
keep FKCLIENT PE_S_DT PE_E_DT age* pl_days epnum BIRTH_DT
gen sty = year(PE_S_DT)
gen endy=year(PE_E_DT)
gen cps_birthy = year(BIRTH_DT)
gen cps_birthm = month(BIRTH_DT)
gen cps_birthd=day(BIRTH_DT)
drop BIRTH_DT
gen nextbdate = mdy(cps_birthm,cps_birthd,sty)
replace nextbdate = mdy(cps_birthm,cps_birthd,sty+1) if nextbdate<=PE_S_DT 
gen lastbdate = mdy(cps_birthm,cps_birthd,endy)
replace lastbdate = mdy(cps_birthm,cps_birthd,endy-1) if lastbdate>=PE_E_DT 

drop sty endy
reshape wide nextbdate lastbdate PE_S_DT PE_E_DT age_at_pl age_end_pl pl_days, i(FKCLIENT) j(epnum)
preserve 
keep FKCL PE_S_DT* PE_E_DT*
save "FCwide",replace
restore 


forval x = 0/17 {
	gen anyplage`x' = 0
	forval y = 1/11 {
		replace anyplage`x' = 1 if age_at_pl`y'==`x'

	}
}

*starting age x and ending age the same
*starting at age x but ending age x+1 (make sure to add days to both age x and age x+1) A -
*starting before and ending after age x - need to add 365 to x, plus portions to each of the starting and ending years
* end date missing - still in care. replace end date = last possible date - dec 31 2020 


forval x = 0/18 {
	gen daysplage`x' = 0
}

		// add difference between end date and start date of placement y if placement y starts and ends at same age 
		//add difference between next bday and start date (= time from placement to birthday) if placement ends one year after start 
		//add difference between end date and next birthday to age X+1 (= time from next birthday to end of placement ) if placement ends one year after start 

forval x = 0/17 {
	local z = `x'+1
	forval y = 1/11 {
		replace daysplage`x' = daysplage`x' + (PE_E_DT`y' - PE_S_DT`y') if age_at_pl`y'==age_end_pl`y'  & age_at_pl`y'==`x'
		
		replace daysplage`x' = daysplage`x' + (nextbdate`y'  - PE_S_DT`y') if age_at_pl`y'==age_end_pl`y'-1 & age_at_pl`y'==`x' 
		
		replace daysplage`z' = daysplage`z' + (PE_E_DT`y' - nextbdate`y') if age_at_pl`y'==age_end_pl`y'-1 & age_at_pl`y'==`x' 	
						
				
		replace daysplage`x' = 365 if age_end_pl`y'> `x' & age_at_pl`y'<`x' & age_at_pl`y'!=. & age_end_pl`y'!=.
	}
}

//add difference between next bday and start date (= time from placement to birthday) if placement ends 2 years after start 
//add difference between end date and birthday right before enddate to age X+k (= time from next birthday to end of placement ) if placement ends k years after start 



forval x = 0/17 {
	local f = 18-`x'
	forval k = 2/`f' {
	local z = `x'+`k'
	forval y = 1/11 {

		replace daysplage`x' = daysplage`x' + (nextbdate`y'  - PE_S_DT`y') if age_at_pl`y'==age_end_pl`y'-`k' & age_at_pl`y'==`x' 
		
		replace daysplage`z' = daysplage`z' + (PE_E_DT`y' - lastbdate`y') if age_at_pl`y'==age_end_pl`y'-`k' & age_at_pl`y'==`x' 	
							
	}
}
}




forval x = 0/17 {
	replace daysplage`x' = 0 if daysplage`x'<0
}

drop PE_* age_* pl_days* nextbdate* lastbdate*
ren FKCL fkclient_t
save "CPS_FCID",replace


